﻿using System.Xml;
using Castle.Core.Configuration;
using Castle.Core.Configuration.Xml;
using System.Collections.Generic;

namespace Inovout.Report
{
    public class ReportDataSourceSet
    {
        public ReportDataSourceSet(XmlReader xmlReader)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(xmlReader);
            IConfiguration dataSourceSetConfiguration = XmlConfigurationDeserializer.GetDeserializedNode(xmlDocument.DocumentElement);
            foreach (var dataSourceConfiguration in dataSourceSetConfiguration.Children)
            {
                string dataSourceType = dataSourceConfiguration.Attributes["Type"];
                switch (dataSourceType.ToLower())
                {
                    case "database":
                        DataSource dataSource = new DatabaseDataSource(dataSourceConfiguration);
                        dataSources.Add(dataSource.Name, dataSource);
                        break;
                    default:
                        throw new System.NotSupportedException(dataSourceType);
                }
            }
        }
        private Dictionary<string, DataSource> dataSources = new Dictionary<string, DataSource>();

        public ReportDataSet[] GetReportData()
        {
            ReportDataSet[] dataSourceSets = new ReportDataSet[dataSources.Count];
            int index = 0;
            foreach (var dataSourceDic in dataSources)
            {
                DataSource dataSource = dataSourceDic.Value;
                dataSourceSets[index] = new ReportDataSet(dataSource, dataSource.GetReportData());
                index++;
            }
            return dataSourceSets;
        }
    }
}
